package com.emep.emeptime.service;

import com.emep.emeptime.entity.AqiIactorInfo;
import com.emep.emeptime.entity.SysAreaInfo;
import com.emep.emeptime.model.AqidataBeanComparator;
import com.emep.emeptime.model.ChinaRankings;
import com.emep.emeptime.model.ChinaSheng;
import com.emep.emeptime.repository.AQIIactorInfoRepository;
import com.emep.emeptime.repository.SysAreaInfoRepository;
import com.emep.emeptime.utils.DateTimeUtil;
import com.emep.emeptime.utils.DateUtils;
import com.emep.emeptime.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author xieyang
 * @Description: aqi地区排名表业务层
 * @date 2018/3/278:57
 * @version V1.0
 */
@Service
@Slf4j
public class AQIIactorInfoService {


    @Resource
    private AQIIactorInfoRepository aqiIactorInfoRepository;

    @Resource
    private SysAreaInfoService sysAreaInfoService;

    /**
     *
     * 新增全国排名数据
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public void saveChina(List<ChinaRankings> payload) {
        ChinaRankings aqidataBean1 = new ChinaRankings();
        int rankings = 0;
        int ranking = 0;
        String date_str = null;
        String date_new = DateTimeUtil.getNowDateTimeForString("yyyy-MM-dd HH");
        String date_new_all = date_new+":00:00";
        try {
            //2+26数组
            List<ChinaRankings> listaqidataBean = new ArrayList<ChinaRankings>();
            //全国的数据
            List<ChinaRankings> aqidataBeanList =  payload;
            List<ChinaRankings> aqidataBeanList_new = new ArrayList<ChinaRankings>();
            for(int i = 0; i < aqidataBeanList.size(); i++){
                ChinaRankings aqidataBean = aqidataBeanList.get(i);
                String datt = aqidataBean.getTime_point().replace("T"," ").replace("Z","");
                if(datt.equals(date_new_all)) {
                    aqidataBeanList_new.add(aqidataBean);
                }
            }
            AqidataBeanComparator comparator= new AqidataBeanComparator();
            Collections.sort(aqidataBeanList_new,comparator);
            for(int i = 0; i < aqidataBeanList_new.size(); i++){
                ChinaRankings aqidataBean = aqidataBeanList_new.get(i);
                if (aqidataBean != null && !StringUtils.isEmpty(aqidataBean.getArea())){
                        if (aqidataBean.getArea().equals("长治")){
                            date_str = aqidataBean.getTime_point().replace("T"," ").replace("Z","");
                            ranking = i+1;
                            aqidataBean1 = aqidataBean;
                            break;
                        }else {
                            continue;
                        }
                    }
            }
            //找出2+26 的城市
            for (int i = 0; i < aqidataBeanList.size(); i++) {
                ChinaRankings aqidataBean = aqidataBeanList.get(i);
                if (aqidataBean.getArea().equals("北京")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("天津")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("石家庄")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("唐山")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("廊坊")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("保定")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("沧州")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("衡水")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("邢台")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("邯郸")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("太原")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("阳泉")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("长治")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("晋城")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("济南")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("淄博")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("济宁")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("德州")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("聊城")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("滨州")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("郑州")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("菏泽")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("开封")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("安阳")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("鹤壁")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("新乡")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("焦作")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("濮阳")){
                    listaqidataBean.add(aqidataBean);
                    continue;
                }else{
                    continue;
                }
            }
            Collections.sort(listaqidataBean,comparator);
            for(int i = 0; i < listaqidataBean.size(); i++){
                ChinaRankings aqidataBean = listaqidataBean.get(i);
                if (aqidataBean != null && !StringUtils.isEmpty(aqidataBean.getArea())){
                    if (aqidataBean.getArea().equals("长治")){
                        rankings = i+1;
                        aqidataBean1 = aqidataBean;
                        break;
                    }else {
                        continue;
                    }
                }
            }

            /**************************************************************************************
             *                 开始添加全国排序
             **************************************************************************************/
            String code = getAreaId(aqidataBean1.getArea());
            AqiIactorInfo aqiIactorInfo = new AqiIactorInfo();
            aqiIactorInfo.setAqi(String.valueOf(aqidataBean1.getAqi()));
            aqiIactorInfo.setArea(code);
            aqiIactorInfo.setRanking(String.valueOf(ranking));
            aqiIactorInfo.setTimePoint(String.valueOf(DateUtils.dateToUnixTimestamp(date_new_all)));
            aqiIactorInfo.setTotal("372");
            //全国 0   全省 1   2+26 2
            aqiIactorInfo.setType("0");
            //保存全国
            AqiIactorInfo  SysAqiInfo = aqiIactorInfoRepository.save(aqiIactorInfo);
            if(SysAqiInfo !=null && !StringUtils.isEmpty(SysAqiInfo.getId())){
                log.info("********************  长治全国排名成功 ******************************");
            }
            /**************************************************************************************
             *                 开始2+26排名
             **************************************************************************************/
            AqiIactorInfo aqiIactorInfo2 = new AqiIactorInfo();
            aqiIactorInfo2.setAqi(String.valueOf(aqidataBean1.getAqi()));
            aqiIactorInfo2.setArea(code);
            aqiIactorInfo2.setRanking(String.valueOf(rankings));
            aqiIactorInfo2.setTimePoint(String.valueOf(DateUtils.dateToUnixTimestamp(date_new_all)));
            aqiIactorInfo2.setTotal("28");
            //全国 0   全省 1   2+26 2
            aqiIactorInfo2.setType("2");
            //保存全国
            AqiIactorInfo  SysAqiInfo2 = aqiIactorInfoRepository.save(aqiIactorInfo2);
            if(SysAqiInfo2 !=null && !StringUtils.isEmpty(SysAqiInfo2.getId())){
                log.info("********************  2+26排名成功 ******************************");
            }
            /**************************************************************************************
             *                 开始添加省排序
             **************************************************************************************/
            AqiIactorInfo aqiIactorInfo1 = new AqiIactorInfo();
            aqiIactorInfo1.setAqi(String.valueOf(aqidataBean1.getAqi()));
            aqiIactorInfo1.setTimePoint(String.valueOf(DateUtils.dateToUnixTimestamp(date_new_all)));
            aqiIactorInfo1.setArea(code);
            List<ChinaRankings> shanxiList = new ArrayList<ChinaRankings>();
            for (int i = 0; i < aqidataBeanList.size(); i++) {
                ChinaRankings aqidataBean = aqidataBeanList.get(i);
                if (aqidataBean.getArea().equals("太原")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("大同")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("阳泉")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("长治")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("晋城")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("朔州")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("晋中")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("运城")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("忻州")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if (aqidataBean.getArea().equals("临汾")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else if(aqidataBean.getArea().equals("吕梁")) {
                    shanxiList.add(aqidataBean);
                    continue;
                }else {
                        continue;
                }
            }
            Collections.sort(shanxiList,comparator);
            for(int i = 0; i < shanxiList.size(); i++){
                ChinaRankings aqidataBean = shanxiList.get(i);
                if (aqidataBean != null && !StringUtils.isEmpty(aqidataBean.getArea())){
                    if (aqidataBean.getArea().equals("长治")){
                        rankings = i+1;
                        break;
                    }else {
                        continue;
                    }
                }
            }
            aqiIactorInfo1.setRanking(String.valueOf(rankings));
            aqiIactorInfo1.setTotal("11");
            aqiIactorInfo1.setType("1");
            AqiIactorInfo  SysAqiInfo1 = aqiIactorInfoRepository.save(aqiIactorInfo1);
            if(SysAqiInfo1 !=null && !StringUtils.isEmpty(SysAqiInfo1.getId())){
                log.info("********************  长治全省排名成功 ******************************");
            }
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
    }
    /**
     * 获取行政区划code
     * @param name
     * @return
     */
    public String getAreaId(String name){
        String areaCode = "";
        try {
            SysAreaInfo sysAreaInfo = sysAreaInfoService.findAreaByAreaName("%" + name + "%");
            if(sysAreaInfo != null ) {

                areaCode = sysAreaInfo.getAreaCode();
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return areaCode;
    }
}
